// pages/fawnIdleHome/fawnIdleHome.js
import api from "../../config/api";
import {
  shareOptions,
  getUserLocation,
  getUserPermissions
} from "../../utils/access";
import request from "../../utils/request";
import {
  jump,
  getBarAndTitleHeight,
  getStorage
} from "../../utils/utils";
const app = getApp();
let Qqmapwx = require('../../utils/qqmap-wx-jssdk.min.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 信号栏高度
    ...getBarAndTitleHeight(),
    // 类别数据
    typeList: [],
    // 选中标签项
    current: 1,
    page: 1,
    total: 0,
    isDone: false,
    // 列表数据
    list: [],
    // 页面滚动距离
    scrollTop: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      ...getBarAndTitleHeight()
    })
    this.miniGoodsDataLogSave(1)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getLabelList()
    this.setData({
      page: 1,
      isDone: false
    })
    this.getList(1)
  },
  // 跳转分类
  jumpClassify(e) {
    let {
      type,
      id,
      title
    } = e.currentTarget.dataset
    if (type === 'more') {
      jump('fawnIdleClassify')
    } else {
      jump('fawnIdleClassifyGoods', {
        id,
        title
      })
    }
  },
  //切换标签
  tabChange(e) {
    if (this.data.current === e.currentTarget.dataset.current) {
      return
    }
    // 位置授权
    console.log('------',getStorage('addressInfo').adcode)
    if (e.currentTarget.dataset.current === 2 && (!getStorage('addressInfo').adcode)) {
      if(this.loading) return
      this.loading = true
      getUserPermissions('userLocation', (res) => {
        if (res) {
          console.log(res)
          this.setData({
            current: e.currentTarget.dataset.current,
            page: 1,
            isDone: false
          })
          this.getList(1)
        }
        this.loading = false
      })
      return
    }
    // if (e.currentTarget.dataset.current === 2) {
    //     let _this = this;
    //     let qqmapsdk = new Qqmapwx({
    //       key: '4NLBZ-BCSCU-27IVS-2DBBT-BCII3-FFBSZ'
    //     })
    //     wx.getLocation({
    //       type: 'gcj02',
    //       success: (res) => {
    //         qqmapsdk.reverseGeocoder({
    //           location: {
    //             latitude: res.latitude,
    //             longitude: res.longitude,
    //           },
    //           success: function (res) {//成功后的回调
    //             _this.setData({
    //               areaCode: res.result.ad_info.adcode,
    //               current: e.currentTarget.dataset.current,
    //               page: 1,
    //               isDone: false
    //             });
    //             _this.getList(1)
    //           },
    //           fail: function (error) {
    //             console.e(error);
    //           },
    //           complete: function (res) {
    //             console.log(res);
    //           }
    //         })
    //       },
    //       fail: error=> {
    //         if(this.data.areaCode){
    //           _this.setData({
    //             current: e.currentTarget.dataset.current,
    //             page: 1,
    //             isDone: false
    //           });
    //           _this.getList(1)
    //         }
    //       },
    //       complete: res=> {
    //         console.log(res);
    //       }
    //     })
    //   return
    // }
    this.setData({
      current: e.currentTarget.dataset.current,
      page: 1,
      isDone: false
    })
    this.getList(1)
  },
  // 获取热门标签
  async getLabelList() {
    let res = await request.get(api.miniGoodsLabelMany, {}, {
      loading: false
    })
    if (res.httpStatus === 200) {
      this.setData({
        typeList: res?.data
      })
    }
  },
  // 获取数据
  async getList(page) {
    let params = {
      pageIndex: page,
      pageSize: 10,
      type: this.data.current,
      sortType: 10
    }
    if (this.data.current === 2) {
      let areaCode = getStorage('addressInfo').adcode
      params.areaCode = areaCode
    }
    let res = await request.get(api.miniGoodsPage, params, { loading: false })
    if (res.httpStatus == 200) {
      this.setData({
        list: page === 1 ? res?.data?.records : [
          ...this.data.list,
          ...res?.data?.records,
        ],
        total: res?.data?.pages
      })
    }
    wx.stopPullDownRefresh()
  },
  // 跳转至搜索页
  jumpSearch() {
    jump('fawnIdleSearch')
  },
  // 访问数据统计
  miniGoodsDataLogSave(type) {
    request.post(api.miniGoodsDataLogSave, {
      type
    }, {
      loading: false
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      isDone: false,
    })
    this.getList(1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {
      page,
      total
    } = this.data
    if (page < total) {
      page++
      this.setData({
        page
      })
      this.getList(page)
    } else {
      this.setData({
        isDone: true
      })
    }
  },
  onPageScroll(e) {
    let Top = e.scrollTop
    this.setData({
      scrollTop: Top
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    return shareOptions('default', e, '身边的二手闲置交易平台', '', null)
  }
})